﻿/****************************************************************************
 * Application   : Freekick Desktop Manager
 * Author        : sfreekick
 * Email         : sfreekick@gmail.com
 * Copyright     : (C) 2008 sfreekick
 * **************************************************************************/

/****************************************************************************
 *  This file is part of FreeKickManager.
 *
 *  FreeKickManager is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  FreeKickManager is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with FreeKickManager. If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Xml.Serialization;

namespace FreeKickManager
{
    [Serializable]
    public class ListViewSettings
    {
        //Nombre de la vista
        public string nombreVista = "";

        //Información de las columnas
        public LinkedList<ListViewColumn> columnasJugadores = new LinkedList<ListViewColumn>();
        public LinkedList<ListViewColumn> columnasPorteros = new LinkedList<ListViewColumn>();

        public ListViewSettings(string nombre, ListView lvj, ListView lvp)
        {
            nombreVista = nombre;

            //Jugadores
            for (int i = 0; i < lvj.Columns.Count; i++)
            {
                ColumnHeader col = lvj.Columns[i];

                ListViewColumn lvc = new ListViewColumn(col.Text, col.Width, col.DisplayIndex);

                columnasJugadores.AddLast(lvc);
            }

            //Porteros
            for (int i = 0; i < lvp.Columns.Count; i++)
            {
                ColumnHeader col = lvp.Columns[i];

                ListViewColumn lvc = new ListViewColumn(col.Text, col.Width, col.DisplayIndex);

                columnasPorteros.AddLast(lvc);
            }
        }

        public void establecerVista(ListView lvj, ListView lvp)
        {
            lvj.BeginUpdate();
            lvp.BeginUpdate();

            //Ocultamos todas las columnas
            foreach (ColumnHeader ch in lvj.Columns)
            {
                ch.Width = 0;
            }

            foreach (ColumnHeader ch in lvp.Columns)
            {
                ch.Width = 0;
            }

            //Recuperamos la configuración de la lista (ancho y posicion de cada columna)
            foreach(ListViewColumn lvc in columnasJugadores)
            {
                ColumnHeader ch = null;
                foreach (ColumnHeader chaux in lvj.Columns)
                {
                    if (chaux.Text.Equals(lvc.cabecera))
                    {
                        ch = chaux;
                        break;
                    }
                }

                if (ch != null)
                {
                    ch.Width = lvc.ancho;
                    ch.DisplayIndex = lvc.orden;
                }
            }

            foreach (ListViewColumn lvc in columnasPorteros)
            {
                ColumnHeader ch = null;
                foreach (ColumnHeader chaux in lvp.Columns)
                {
                    if (chaux.Text.Equals(lvc.cabecera))
                    {
                        ch = chaux;
                        break;
                    }
                }

                if (ch != null)
                {
                    ch.Width = lvc.ancho;
                    ch.DisplayIndex = lvc.orden;
                }
            }

            lvj.EndUpdate();
            lvp.EndUpdate();
        }

        public override string ToString()
        {
            return this.nombreVista;
        }
    }

    [Serializable]
    public class ListViewColumn
    {
        public string cabecera;
        public int ancho;
        public int orden;

        public ListViewColumn(string colCabecera, int colAncho, int colOrden)
        {
            cabecera = colCabecera;
            ancho = colAncho;
            orden = colOrden;
        }
    }
}
